package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.DaRenAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.DaRenBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FilterListActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;

    private DaRenAdapter mAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;

    private int page=1;
    private String minage,maxage,longi,lat,ditance,labelid,sex,search,filterTimes;
    private int labelclassid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter_list);
        ButterKnife.bind(this);
        initView();
    }
    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("附近");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new DaRenAdapter(this);
        mAdapter.setDataList(new ArrayList<DaRenBean.BodyBean>());
         lRecyclerViewAdapter=new LRecyclerViewAdapter(mAdapter);
        //divider
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.size_1)
//                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.bg)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(true);
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getDatas();
            }
        });
        recyclerView.forceToRefresh();
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent i=new Intent(FilterListActivity.this, AttentionUserClassifyActivity.class);
                i.putExtra("listId",mAdapter.getDataList().get(position).getListid()+"");
                startActivity(i);
            }
        });
    }

    private void refresh(){
        page=1;
        mAdapter.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();//必须调用此方法
        getDatas();
    }

    private void getDatas() {
        OkgoUtil.post(Http.CLASS_WORK_LSIT,FilterListActivity.this)
                .params("pageno",page)
                .params("minage",minage)
                .params("maxage",maxage)
                .params("long",longi)
                .params("lat",lat)
                .params("distance",ditance)
                .params("labelclassid",labelclassid)
                .params("labelid",labelid)
                .params("sex",sex)
                .params("search",1)
                .params("time",filterTimes)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DaRenBean parentBean= DealResult.getInstance().dealDatas(FilterListActivity.this,response,new TypeToken<DaRenBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS){
                            List<DaRenBean.BodyBean> beans = parentBean.getBody();
                            if(beans.isEmpty()){return;}
                            mAdapter.addAll(beans);
                            recyclerView.refreshComplete(10);
                        }else {
                            if(code==Constant.EMPTY){
                                if(page==1){
                                    ToastUtils.toastForShort(FilterListActivity.this,"数据为空");
                                }else {
                                    recyclerView.setNoMore(true);
                                }
                            }else{
                                ToastUtils.toastForShort(FilterListActivity.this,parentBean.getResult());
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        recyclerView.refreshComplete(mAdapter.getItemCount());
                        ToastUtils.toastForShort(FilterListActivity.this,getString(R.string.bad_net));
                    }
                });
    }


    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_middle_title:
                break;
        }
    }
}
